package com.design.pattern.component.generation;

import java.util.ArrayList;
import java.util.List;

/**
 * 族谱中的男人,男人会有妻子,女儿和儿子
 */
public class Man extends Genealogy {
    /**
     * 亲人集合
     */
    private List<Genealogy> Relatives = new ArrayList<>();

    public Man(String name, Integer generation, String Identity) {
        super(name, generation, Identity);
    }
    public Man(String name, Integer generation) {
        super(name, generation);
    }
    public Genealogy add(Genealogy genealogy,String Identity) {
        genealogy.setIdentity(Identity);
        Relatives.add(genealogy);
        return this;
    }

    public Genealogy delete(Genealogy genealogy) {
        Relatives.remove(genealogy);
        return this;
    }

    public String show() {
        StringBuffer buffer = new StringBuffer();
        for (Genealogy genealogy : Relatives) {
            //如果是男性,继续往下追溯
            if (genealogy instanceof Man) {
                for (int i = 0; i < genealogy.getGeneration(); i++) {
                    //处理格式
                    buffer.append("\t");
                }
                buffer.append(genealogy.getIdentity() + ":" + genealogy.getName() + "\r\n");
                buffer.append(((Man) genealogy).show());
            }else {
                buffer.append(genealogy.show());
            }
        }
        return buffer.toString();
    }

}
